package com.txzt.gaea.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;


    public String get(String key){

        return stringRedisTemplate.opsForValue().get(key);
    }
    public long Stringincr(String key){
        return stringRedisTemplate.opsForValue().increment(key);
    }

    public void set(String key, String value){
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public void set(String key, String value, Long seconds){
        stringRedisTemplate.opsForValue().set(key, value);
        stringRedisTemplate.expire(key,seconds, TimeUnit.SECONDS);
    }
    public boolean setIfAbsent(String key, String value, Long seconds){
        return stringRedisTemplate.opsForValue().setIfAbsent(key,value,seconds, TimeUnit.SECONDS);
//        return stringRedisTemplate.opsForValue().setIfAbsent(key, value);
    }
    public void expire(String key, Long seconds){
        stringRedisTemplate.expire(key,seconds, TimeUnit.SECONDS);
    }

    public void remove(String key){
        stringRedisTemplate.delete(key);
    }

    public boolean delete(String key){
        boolean isDeteled = stringRedisTemplate.delete(key);
        return isDeteled;
    }

    public void deleteObject(String key) {
        this.redisTemplate.delete(key);
    }
}
